import Vue from 'vue'
import VueRouter from 'vue-router'
import Cookies from 'js-cookie'
import axios from "axios";

Vue.use(VueRouter)

import HomePage from "@/components/HomePage";
import Hotel from "@/components/Hotel.vue";
import Login from "@/components/Login";
import Fiight from "@/components/Flight.vue";
import HotelDetail from "@/components/HotelDetail.vue";
import OrderPage from "@/components/OrderPage.vue";
import FlightOrder from "@/components/FlightOrder.vue";
import TrainOrder from "@/components/TrainOrder.vue";
import RentCarOrder from "@/components/RentCarOrder.vue";
import HotelOrder from "@/components/HotelOrder.vue";
import HotelCreateOrder from "@/components/HotelCreateOrder.vue";
import RentalCars from "@/components/RentalCars";
import RentalCarsDetail from "@/components/RentalCarsDetail";

const routes = [

    {
        path: '/',
        name: 'HomePage',
        component: HomePage,
        children: [
            {
                //酒店页
                path: '',
                name: 'Hotel',
                component: Hotel,
            },
            {
                //机票页
                path: '/Flight',
                name: 'Flight',
                component: Fiight,
            },
            {
                //酒店详情页
                path: '/HotelDetail/:hotelId', // 这里我们添加了一个动态路由参数 hotelId
                name: 'HotelDetail',
                component: HotelDetail,
            },
            {
                //酒店详情页
                path: '/HotelCreateOrder/',
                name: 'HotelCreateOrder',
                component: HotelCreateOrder,
            },
            {
                //租车详情页
                path: '/RentalCars',
                name: 'RentalCars',
                component: RentalCars,
            },
            {
                //租车详情页
                path: '/RentalCarsDetail',
                name: 'RentalCarsDetail',
                component: RentalCarsDetail,
            }
        ]

    },
    {
        path: '/Order',
        name: 'OrderPage',
        component: OrderPage,
        children: [
            {
                //酒店订单
                path: '', // Default child path
                name: 'HotelOrder',
                component: HotelOrder,
            },
            {
                //机票订单
                path: 'FlightOrder',
                name: 'FlightOrder',
                component: FlightOrder,
            },
            {
                //火车票订单
                path: 'TrainOrder',
                name: 'TrainOrder',
                component: TrainOrder,
            },
            {
                //租车订单
                path: 'RentCarOrder',
                name: 'RentCarOrder',
                component: RentCarOrder,
            },
        ]
    },
    {
        path: '/Login',
        name: 'Login',
        component: Login
    }

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes

})

//检查登录状态
async function checkUserExistence() {
    const token = Cookies.get('user');

    if (!token) {
        //没有token,返回false
        return false;
    }
    //有token,認為已經是登錄狀態了,返回true
    return true;
}

//检查用户权限
async function getUserPermission() {
    const token = Cookies.get('user');
    const params = {
        token: token
    }
    return axios.post('/userinfo/analysis', params).then(res => {
        return res.data.data.permission;
    });
}

export default router
